package com.tianyu.inspection;

import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2020.12.11 - 10:11
 **********************************/


@SpringBootApplication
@EnableScheduling
@Slf4j
@ControllerAdvice
@EnableAsync
public class MainApplication {

	public static void main(String[] args) {
		SpringApplication.run(MainApplication.class,args);
	}

	/**
	 * 捕获 Controller 中抛出的指定类型的异常，也可以指定其他异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
	public void e1(Exception e){
		log.error("HTTP请求路径错误...");
	}

	@ExceptionHandler(value = HttpMessageNotWritableException.class)
	public void e2(Exception e){
		log.debug("JSON转换错误...");
	}

	@ExceptionHandler(value = {DataIntegrityViolationException.class, MysqlDataTruncation.class})
	public void e3(Exception e){
		log.error("数据库错误..."+e.getMessage());
	}

	@ExceptionHandler(value = {MultipartException.class})
	public void e4(Exception e){
		log.error("文件上传错误...");
	}
}
